package pxene.test.logicregression

import java.util.regex.PatternSyntaxException

import org.apache.log4j.Level
import org.apache.log4j.Logger
import org.apache.spark.SparkConf
import org.apache.spark.SparkContext
import org.apache.spark.mllib.classification.LogisticRegressionWithLBFGS
import org.apache.spark.mllib.linalg.Vectors
import org.apache.spark.mllib.regression.LabeledPoint

object LogicRegressionTrain {
  def main(args: Array[String]): Unit = {
    // 屏蔽不必要的日志显示终端上
    Logger.getLogger("org.apache.spark").setLevel(Level.ERROR)
    Logger.getLogger("org.eclipse.jetty.server").setLevel(Level.OFF)

    // 设置运行环境
    val conf = new SparkConf().setAppName("regression")
    val sc = new SparkContext(conf)

    // Load and parse the data
    val data = sc.textFile("file:///home/chenjinghui/regression/regression_train.txt")
    val parsedData = data.map { line =>
      val parts = line.split(',')
      LabeledPoint(parts(0).toDouble, Vectors.dense(parts(1).split(' ').map(_.toDouble)))
    }

    val splits = parsedData.randomSplit(Array(0.7, 0.3), seed = 11L)
    val trainingData = splits(0)
    val testData = splits(1)

    val model = new LogisticRegressionWithLBFGS().setNumClasses(2).run(trainingData)
    val labelAndPreds = testData.map { point =>
      val prediction = model.predict(point.features)
      (point.label, prediction)
    }

    val trainErr = labelAndPreds.filter(r => r._1 != r._2).count.toDouble / testData.count

    println("----train error----" + trainErr)

    model.save(sc, "file:///home/chenjinghui/regression/model")
    
    sc.stop()
  }

  def getDoubleValue(input: String): Double = {
    var result: Double = 0.0
    if (input == "0101") result = 0.0
    if (input == "0102") result = 1.0
    if (input == "0103") result = 2.0
    if (input == "0104") result = 3.0
    if (input == "0201") result = 4.0
    return result
  }
}